package com.ruoyi.sywx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sywx.domain.bo.SywxYqFeedbackBo;
import com.ruoyi.sywx.domain.vo.SywxYqFeedbackVo;
import com.ruoyi.sywx.domain.SywxYqFeedback;
import com.ruoyi.sywx.mapper.SywxYqFeedbackMapper;
import com.ruoyi.sywx.service.ISywxYqFeedbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 舆情反馈Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@RequiredArgsConstructor
@Service
public class SywxYqFeedbackServiceImpl implements ISywxYqFeedbackService {

    private final SywxYqFeedbackMapper baseMapper;

    /**
     * 查询舆情反馈
     */
    @Override
    public SywxYqFeedbackVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询舆情反馈列表
     */
    @Override
    public TableDataInfo<SywxYqFeedbackVo> queryPageList(SywxYqFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SywxYqFeedback> lqw = buildQueryWrapper(bo);
        Page<SywxYqFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询舆情反馈列表
     */
    @Override
    public List<SywxYqFeedbackVo> queryList(SywxYqFeedbackBo bo) {
        LambdaQueryWrapper<SywxYqFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SywxYqFeedback> buildQueryWrapper(SywxYqFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SywxYqFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getYqId() != null, SywxYqFeedback::getYqId, bo.getYqId());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SywxYqFeedback::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedback()), SywxYqFeedback::getFeedback, bo.getFeedback());
        return lqw;
    }

    /**
     * 新增舆情反馈
     */
    @Override
    public Boolean insertByBo(SywxYqFeedbackBo bo) {
        SywxYqFeedback add = BeanUtil.toBean(bo, SywxYqFeedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatchByBoList(List<SywxYqFeedbackBo> feedbackBos) {
        List<SywxYqFeedback> feedbacks = BeanUtil.copyToList(feedbackBos, SywxYqFeedback.class);
        return baseMapper.insertBatch(feedbacks);
    }

    /**
     * 修改舆情反馈
     */
    @Override
    public Boolean updateByBo(SywxYqFeedbackBo bo) {
        SywxYqFeedback update = BeanUtil.toBean(bo, SywxYqFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SywxYqFeedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除舆情反馈
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<SywxYqFeedback> queryByYqId(Long id) {
        return baseMapper.selectList(Wrappers.lambdaQuery(SywxYqFeedback.class).eq(SywxYqFeedback::getYqId, id));
    }
}
